#include "Transaction.hh"
#include "Utils.hh" 
#include <sstream>
#include <iomanip>
#include <ctime>

using namespace std;

// 静态成员初始化
int Transaction::nextID = 1;

Transaction::Transaction(double amt, const std::string& n, TransactionType t,
                         const std::string& from, const std::string& to)
    : transactionID(nextID++), amount(amt), note(n), type(t),
      fromAccount(from), toAccount(to) {
    timestamp = time(nullptr);
}

std::string Transaction::getFrom() const {
    return fromAccount;
}

std::string Transaction::getTo() const {
    return toAccount;
}

int Transaction::getID() const {
    return transactionID;
}

TransactionType Transaction::getType() const {
    return type;
}

double Transaction::getAmount() const {
    return amount;
}

time_t Transaction::getTime() const {
    return timestamp;
}

string Transaction::getNote() const {
    return note;
}

string Transaction::toString() const {
    // 使用通用工具
    string timeStr = Utils::formatTime(timestamp);
    // 将交易类型从枚举转换为字符串
    string typeStr = Utils::transactionTypeToString(type);
    // 使用ostringstream将信息格式化为字符串
    ostringstream oss;
    oss << "[#" << transactionID << "] "
        << "[" << timeStr << "] "
        << "[" << typeStr << "] "
        << "¥" << fixed << setprecision(2) << amount
        << " | Note: " << note;

    return oss.str();
}
